package com.quxue.im.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.quxue.im.entity.ChatUser;
import com.quxue.im.req.user.*;
import com.quxue.im.vo.AjaxResult;
import com.quxue.im.service.IChatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/my")
public class MyController {

    @Autowired
    private IChatUserService chatUserService;

    @RequestMapping("/getInfo")
    public AjaxResult getInfo(){
        return chatUserService.getInfo();

    }
    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    public AjaxResult logout() {
        StpUtil.logout();
        return AjaxResult.success();
    }


    @RequestMapping("/editPortrait")
    public AjaxResult editPortrait(@RequestBody PortraitReq portraitReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setPortrait(portraitReq.getPortrait());
        chatUserService.updateById(chatUser);

        return AjaxResult.success("修改头像成功");
    }



    @RequestMapping("/editNick")
    public AjaxResult editNick(@RequestBody NickReq nickReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setNickName(nickReq.getNickName());
        chatUserService.updateById(chatUser);
        return AjaxResult.success("修改昵称成功");
    }
    @RequestMapping("/editChatNo")
    public AjaxResult editChatNo(@RequestBody ChatNoReq chatNoReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setChatNo(chatNoReq.getChatNo());
        chatUserService.updateById(chatUser);
        return AjaxResult.success("修改聊天号成功");
    }
    @RequestMapping("/editGender")
    public AjaxResult editGender(@RequestBody GenderReq genderReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setGender(genderReq.getGender());
        chatUserService.updateById(chatUser);
        return AjaxResult.success("修改性别成功");
    }
    @RequestMapping("/editCity")
    public AjaxResult editCity(@RequestBody AddressReq addressReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setProvinces(addressReq.getProvinces());
        chatUser.setCity(addressReq.getCity());
        chatUserService.updateById(chatUser);
        return AjaxResult.success("修改城市成功");
    }
    @RequestMapping("/editIntro")
    public AjaxResult editIntro(@RequestBody IntroReq introReq) {
        ChatUser chatUser = new ChatUser();
        chatUser.setUserId(StpUtil.getLoginIdAsLong());
        chatUser.setIntro(introReq.getIntro());
        chatUserService.updateById(chatUser);
        return AjaxResult.success("修改个性签名成功");
    }

    @RequestMapping("/editPass")
    public AjaxResult editPass(@RequestBody PassReq passReq){
        AjaxResult result = chatUserService.updatePassword(passReq);
        return AjaxResult.success(result);
    }

    /**
     * 刷新
     */
    @GetMapping("/refresh")
    public AjaxResult refresh() {
        System.out.println("MyController.refresh");
        chatUserService.refresh();
        return AjaxResult.success();
    }
}
